package com.yanqu.road.dao.impl.channelcenter.washe;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.washe.data.WasheSceneData;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class WasheSceneDataDaoImpl extends TempDataObjectDao<WasheSceneData> {

    public static String getWasheSceneDataSql = "select * from `t_n_washe_scene` where `channelKey`=?";

    public WasheSceneData getWasheSceneData(String channelKey) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getChannelServerDb());
            call = getDb().getConnection().prepareCall(getWasheSceneDataSql);
            int i = 1;
            call.setString(i++, channelKey);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateWasheSceneDataSql = "update `t_n_washe_scene` set sceneList=?,refreshTime=?,sceneWeightMap=? where channelKey=?";

    @Override
    protected boolean updateData(WasheSceneData washeSceneData) {
        washeSceneData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getChannelServerDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(washeSceneData.getSceneList())));
        para.put(i++, new DbParameter(Types.BIGINT, washeSceneData.getRefreshTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(washeSceneData.getSceneWeightMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, washeSceneData.getChannelKey()));
        if (getDb().executeNoneQuery(updateWasheSceneDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertWasheSceneDataSql = "insert into `t_n_washe_scene`(channelKey,sceneList,refreshTime,sceneWeightMap) values (?,?,?,?)";

    @Override
    protected boolean addData(WasheSceneData washeSceneData) {
        washeSceneData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getChannelServerDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, washeSceneData.getChannelKey()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(washeSceneData.getSceneList())));
        para.put(i++, new DbParameter(Types.BIGINT, washeSceneData.getRefreshTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(washeSceneData.getSceneWeightMap())));
        if (getDb().executeNoneQuery(insertWasheSceneDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected WasheSceneData parseData(ResultSet rs) throws SQLException {
        WasheSceneData washeSceneData = new WasheSceneData();
        washeSceneData.setChannelKey(rs.getString("channelKey"));
        washeSceneData.setSceneList(JSON.parseObject(rs.getString("sceneList"), new TypeReference<List<Integer>>() {
        }));
        washeSceneData.setRefreshTime(rs.getLong("refreshTime"));
        String sceneWeightMap = rs.getString("sceneWeightMap");
        if (!StringUtils.isNullOrEmpty(sceneWeightMap)) {
            washeSceneData.setSceneWeightMap(JSON.parseObject(sceneWeightMap, new TypeReference<ConcurrentHashMap<Integer, Integer>>() {
            }));
        }
        return washeSceneData;
    }

}
